package cc.admin.common.db.table;

import cc.admin.common.db.table.base.EtlReader;
import cc.admin.common.db.table.entity.DbColumn;
import cc.admin.common.db.table.entity.DbEntity;
import cc.admin.common.db.util.DbConstants;
import cc.admin.common.db.util.FreemarkerUtil;
import cc.admin.common.db.util.TypeConvertUtil;
import cn.hutool.core.io.FileUtil;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/**
 * @author: ZhangHouYing
 * @date: 2018-06-24
 */
@Slf4j
public class MakeCreateTable {

	public static String getTableSql(DbEntity dbEntity, String template) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("typeConvertUtil", new TypeConvertUtil());
		map.put("entity", dbEntity);
		map.put("columnList", dbEntity.getDbColumnList());
		map.put("pkList", dbEntity.getPkList());
		String createTableSql = FreemarkerUtil.renderContent(template, map);
		return createTableSql;
	}

	public static String getCreateSql(String dbKey,DbEntity dbEntity){
		String template = null;
		String dbType = EtlReader.getInstance(dbKey).getDbType();
		switch (dbType){
			case DbConstants.DB_NAME_MYSQL :
				template = "mysql.ftl";
				break;
			case DbConstants.DB_NAME_ORACLE :
				template = "oracle.ftl";
				break;
			case DbConstants.DB_NAME_SQLSERVER :
				template = "sqlServer.ftl";
				break;
			case DbConstants.DB_NAME_HIVE:
				template = "hive.ftl";
				break;
			default : //可选
				throw new RuntimeException("不能识别的etlReader:"+dbType);
		}
		return getTableSql(dbEntity,template);
	}

	public static void writeSqlListToFile(List<String> dropSqlList,List<String> createSqlList){
		List<String> lowerCaseSqlList = Lists.newArrayList();
		createSqlList.forEach(sql->{
			sql = sql.toLowerCase();
			sql = sql.replaceAll("\n", " ");
			sql = sql.replaceAll("\r", " ");
			lowerCaseSqlList.add(sql);
		});
		FileUtil.writeLines(dropSqlList, "D:\\opt\\tmp\\drop.sql", Charsets.UTF_8);
		FileUtil.writeLines(createSqlList, "D:\\opt\\tmp\\create.sql", Charsets.UTF_8);
	}

	public static void writeColumnsToFile(List<DbEntity> resultList) {
		List<String> columnStrList = Lists.newArrayList();
		for (DbEntity dbEntity : resultList) {
			String entity = dbEntity.getId().toLowerCase();
			StringBuilder sb = new StringBuilder();
			List<DbColumn> columnList = dbEntity.getDbColumnList();
			for (int i = 0; i < columnList.size(); i++) {
				DbColumn dbColumn = columnList.get(i);
				if (i > 0) {
					sb.append(",");
				}
				sb.append(dbColumn.getCode());
			}
			columnStrList.add(String.format("%s : %s", entity, sb.toString().toLowerCase()));
		}
		FileUtil.writeLines(columnStrList, "D:\\opt\\tmp\\columns.sql", Charsets.UTF_8);
	}

	/**
	 * 把oracle 建表语句转换为 hive的
	 * @throws SQLException
	 */
	public static void generateSourceToDist(String sourceKey,String distKey) throws SQLException {
		EtlReader etlReader = EtlReader.getInstance(sourceKey);
		etlReader.init();
		List<DbEntity> dbEntityList = etlReader.readEntity();
		List<String> dropSqlList = Lists.newArrayList();
		List<String> createSqlList = Lists.newArrayList();
		for (DbEntity dbEntity : dbEntityList) {
			String createSql = getCreateSql(distKey,dbEntity);
			createSqlList.add(createSql);
			dropSqlList.add(String.format("drop table %s;", dbEntity.getId()));
			System.out.println(createSql);
			writeColumnsToFile(dbEntityList);
			writeSqlListToFile(dropSqlList,createSqlList);
		}
	}

	public static void main(String[] args) throws SQLException {
		generateSourceToDist("cc-admin","");
	}
}
